//如果登录 改变布局
function changeLogin(){
    let login=JSON.parse(localStorage.getItem('login')||"[]")
    let iflogin=document.querySelector('.iflogin')
    if(login!=''){
     
       iflogin.innerHTML=`用户<a href='#'>${login.username}</a>`
       iflogin.nextSibling.innerHTML=`<a href='./cart.html'>我的订单</a>`
    }else{
        iflogin.innerHTML=`<a href="./login.html">登录</a>`
        iflogin.nextSibling.innerHTML=`<a href="./register.html">请注册</a>`
    }   
}
changeLogin()
//点击广告消失
let eliminate=document.querySelector('.eliminate');
eliminate.onclick=function(){
    
    this.parentNode.style.display='none'
}

let pageNo=1;
let allstr='';

//获取数据
function getRecomList(pageNo){
    axios({
        method:'get',
        url:'http://api.yuguoxy.com/api/shop/list',
        params:{
            pageSize:10,
            pageNo
        },
    }).then(response=>{
        let data=response.data
        let{resultCode,resultInfo} = data
        if(resultCode == 1 ){
            let goodlist = resultInfo
            showGoodList(goodlist)
        }
    })
}
//渲染 
function showGoodList(list){
    let product=list.list;
    let str=product.reduce((pre,cur)=>{
        return pre+=`
        <li data-id=${cur.id}>
        <div class="remodlist_img">
            <img src="${cur.picture}" alt="">
        </div>
        <div class="recomlist_text">
          <div class="oneline">  
              <span>价保618</span>
            <span class="spanp">${cur.shop}</span></div>
        
            <span class="recomprice">$${cur.price}</span>
        </div>
     
    </li>
        `
    },'')
    allstr+=str
   
    let productOul=document.querySelector('.remodlist').querySelector('ul')
    productOul.innerHTML=allstr
    clickGood()
}

let more=document.querySelector('.more')
more.onclick=function(){
    getRecomList(++pageNo)
}


let back=document.querySelector('.backTop')
//回到顶部
window.addEventListener('scroll',function(){
    if(this.scrollY>500){
        back.style.display='block'
        back.onclick=function(){
            window.clearInterval(window.timer);
            window.timer = window.setInterval(function() {
              
                var step = (0 -window.pageYOffset) / 10;//缓动效果核心算法
                step = step >= 0 ? Math.ceil(step) : Math.floor(step);
                if (window.pageYOffset == 0) {
                    //停止动画 本质停止定时器
                    window.clearInterval(window.timer);
                    //回调函数一定要写到定时器结束里面
                   
                } else {
                    window.scroll(0,window.pageYOffset+step)
                }
            }, 30)
        }
    }else{
        back.style.display='none'
    }
})


//分类列表

let navList=document.querySelector('.navP').querySelectorAll('a')
for(let i=0;i<navList.length;i++){
    navList[i].setAttribute('data-id',i)
    navList[i].addEventListener('click',function(){
       let index=this.getAttribute('data-id')
       getNavList(index)
    })
}


function getNavList(id){
    axios({
        method:'get',
        url:'http://api.yuguoxy.com/api/shop/list/category',
        params:{
            categoryId:id
        },
    }).then(response=>{
        let data=response.data
        let {resultCode,resultInfo} =data;
        if(resultCode==1){
            let navList=resultInfo.list
            localStorage.setItem('navList',JSON.stringify(navList))
          location.href='../pages/navpage.html'
        }
    })
}



//点击获取参数  保存本地  
//渲染本地
//点击回到首页  清除本地

//点击商品跳转详情
function clickGood(){
    let productOul=document.querySelector('.remodlist').querySelector('ul')
    
    let lis=productOul.children;
    console.log(lis);
    [...lis].forEach(item=>{
        item.addEventListener('click',function(){
            let goodId=this.getAttribute('data-id')
            location.href=`../pages/detail.html?id=${goodId}`
        })
    })
}



//运行
getRecomList()

//退出登录
let exit=document.querySelector('.exit')
exit.onclick=function(){
    if(confirm('退出登录?')){
        localStorage.removeItem('login')
        location.reload();
    }
}


//搜索
let svg=document.querySelector('.svg')
svg.onclick=function(){
    let search=document.querySelector('.search')
    let val=search.value;
    if(val==''){
        alert('请输入搜索内容')
    }else{
        location.href=`../pages/find.html?keyword=${val}`
       
    }
   
    
}